package com.ruoyi.station.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.station.domain.QueryData;
import org.apache.catalina.manager.util.SessionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ParseSql {

    /**
     * 全部数据权限
     */
    public static final String DATA_SCOPE_ALL = "1";

    /**
     * 自定数据权限
     */
//    public static final String DATA_SCOPE_CUSTOM = "2";

    /**
     * 部门数据权限
     */
    public static final String DATA_SCOPE_DEPT = "3";

    /**
     * 部门及以下数据权限
     */
    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";

    /**
     * 仅本人数据权限
     */
    public static final String DATA_SCOPE_SELF = "5";

    public static Set<String> getUserByRoleUtil(List<String> userList){
        Set<String> userSet = new HashSet();//需要查询的人员数据集
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        if (StringUtils.isNotNull(loginUser)){
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()){
                for (SysRole role : currentUser.getRoles()) {
                    String dataScope = role.getDataScope();
                    if (DATA_SCOPE_ALL.equals(dataScope)){
                        userSet.clear();
                        break;
                    }else if (DATA_SCOPE_DEPT.equals(dataScope)){
                        userList.stream().forEach(user -> userSet.add(user));
                    }else if (DATA_SCOPE_SELF.equals(dataScope)){
                        userSet.add(currentUser.getUserName());
                    }
                }
            }
        }
        return userSet;
    }

    /**
     * 将前端传参解析成sql形式
     * @param queryDataList
     * @return
     */
    public static <T> T parse(T t, List<QueryData> queryDataList,List<String> userList){
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        Set<String> userByRoleSet = getUserByRoleUtil(userList);
        if (userByRoleSet != null && userByRoleSet.size() > 0){
            wrapper.in("create_by",userByRoleSet);
        }

        if (queryDataList == null || queryDataList.size() == 0) return (T)wrapper;
        queryDataList.stream().forEach(queryData -> {
            switch (queryData.getCondition()){
                case "eq" : //等于
                    wrapper.eq(queryData.getFieldName(),queryData.getVal());
                    break;
                case "ne" : //不等于
                    wrapper.ne(queryData.getFieldName(),queryData.getVal());
                    break;
                case "gt" : //大于
                    wrapper.gt(queryData.getFieldName(),queryData.getVal());
                    break;
                case "ge" : //大于等于
                    wrapper.ge(queryData.getFieldName(),queryData.getVal());
                    break;
                case "lt" : //小于
                    wrapper.lt(queryData.getFieldName(),queryData.getVal());
                    break;
                case "le" : //小于等于
                    wrapper.le(queryData.getFieldName(),queryData.getVal());
                    break;
                case "like" : //包含
                    wrapper.like(queryData.getFieldName(),queryData.getVal());
                    break;
            }
        });
        return (T)wrapper;
    }
}
